<?php
/**
 * 会员收藏夹model
 * @author tangbl
 * 
 */
class MemberCollectModel extends Model {
	
	/*array(
		0.字段名   
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息  {%usr_form_validata_email_email}多国语言
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	);*/
	protected $_validate    =   array(
			
	);
	
	/*array(
	    0.填充字段:字段名
		1.填充规则:方法名或者字段名
		2.填充时间:[1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
		4.附加规则 [function、callback、field 、string ]
	);*/
	protected $_auto    =   array(
			
	);
	
	/**
	 * 获取当前用户的收藏夹列表
	 * @param $userId int 用户编号
	 * return array 二维数组
	 */
	public function getUserCollect($userId){
		$model = D("MemberCollectView");
		
		import('ORG.Util.Page');
		$map['userid'] = $userId;
			
		$count      = $model->where($map)->count();
		$Page       = new Page($count,8);
		$show       = $Page->show();
			
		$data = $model->where($map)->order('id desc')
				->limit($Page->firstRow.','.$Page->listRows)->select();
		
		return array("data"=>$data,"show"=>$show);
	}
	
	/**
	 * 根据id 删除用户收藏夹产品
	 * @param  $map array 用户编号、id
	 * 
	 */
	public function delUserCollect($map){
		
		$data  = $this->where($map)->delete();
		
		return $data;
	}
	
	/**
	 * 检查用户收藏夹，是否超过设定数量20
	 */
	public function checkUserCollect($userid){
		$map['userid']  = $userid;
		$query          = $this->where($map)->select();
		if(count($query)>=20){
			$flag = true;
		}else{
			$flag = false;
		}
		return $flag;
	}
	
	public function addCollect($pid){
		
		$user  = getMember();
		
		$product       = M("Product");
		$map['pid']    = $pid;
		$map['userid'] = $user['id'];
		$view          = $this->where($map)->find();
		
		if($view['id']){
			$flag = false;
		}else{
			$pro                = $product->find($pid);
			$collect['solevar'] = create_id();
			$collect['pid']     = $pid;
			$collect['userid']  = $user['id'];
			$collect['type']    = $user['type'];
			$collect['addtime'] = time();
			$collect['price']   = $pro['sprice'];
			$collect['title']   = $pro['title'];
				
			$product->where("id=".$pid)->setInc("colnum",1);
			$flag = $this->add($collect);
		}
		return $flag;
	}
}
?>